// Fill out your copyright notice in the Description page of Project Settings.

#include "StSocketTool.h"
#include "StSocketClient.h"
#include "FileHelper.h"
#include "Paths.h"




bool UStSocketTool::GetIpInt(int index, int& a, int& b, int& c, int& d, FString& pcName)
{
	unsigned long ips;

	return StSocketClient::GetIp(index, ips,a,b,c,d, pcName);
}


bool UStSocketTool::GetIpString(int index, FString& ip, FString& pcName)
{
	ip.Reset();
	unsigned long ips;
	int a, b, c, d;
	if (StSocketClient::GetIp(index, ips, a, b, c, d, pcName)) 
	{
		ip = FString::Printf(TEXT("%i.%i.%i.%i"), a, b, c, d);
		return true;
	}
	return false;
}



bool UStSocketTool::GetIPFromDns(FString dns_, FString& outip) 
{
	return StSocketClient::GetIPFromDns(dns_, outip);
}

bool UStSocketTool::IsIPv4(FString ip) 
{
	if (ip.Equals("0.0.0.0"))return true;
	int a, b, c, d; 
	TArray<FString> ips;
	ip.ParseIntoArray(ips, TEXT("."));

	if (ips.Num() == 4) 
	{
		a = FCString::Atoi(*(ips[0]));
		b = FCString::Atoi(*(ips[1]));
		c = FCString::Atoi(*(ips[2]));
		d = FCString::Atoi(*(ips[3]));
		if (a > 0 && a <= 255 && b > 0 && b <= 255 && c > 0 && c <= 255 && d > 0 && d <= 255) 
		{
			return true;
		}
	}
	return false;
}

FString UStSocketTool::GetBasePath() 
{
	return FPaths::ProjectDir();
}

FString UStSocketTool::GetBasePathWin() 
{
	FString path = FPaths::ProjectDir();
	path.Replace(TEXT("/"), TEXT("\\"));
	return path;


}


bool UStSocketTool::ReadTxtData(FString path, FString& data)
{
	return 	FFileHelper::LoadFileToString(data, *path);
}


bool UStSocketTool::WriteTxtDataToDisk(FString path, FString data) 
{
	return FFileHelper::SaveStringToFile(data, *path);
}


FString UStSocketTool::GetTimeString()
{
	return FDateTime::Now().ToString();
}

